package com.card.main;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;

import com.card.game.BasePlayer;
import com.card.game.room.AbstractRoom;
import com.card.user.User;
import com.card.user.UserLogic;
import com.card.user.UserManager;
import com.xiaoao.net.global.Global;
import com.xiaoao.net.json.IHanderProcessor;
import com.xiaoao.net.utils.NetUtil;

public class CardHanderProcessor implements IHanderProcessor{
	
	@Override
	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
		try {
			Global.logErrorMessage(NetUtil.getIp(ctx)+"!!!!!!!!!断开连接");
			User user = (User)ctx.getAttachment();
			if(user == null) {
				return;
			}
			User temp = UserManager.getInstance().getUserByKey(user.getPrimaryKey());
			if(user != null && (temp == null || !temp.isOnline())) {
				Global.logErrorMessage("掉线了！！"+user.userExtra().getNickName());
				AbstractRoom room = user.getRoom();
				if(room != null) {
					Global.logErrorMessage("掉线了！！"+user.getPrimaryKey());
					BasePlayer player = room.getPlayerById(user.getPrimaryKey());
					player.changeOnline(false);
				}
				UserLogic.getInstance().userLoginOut(user);
			}
			user.setCtx(null);
		} catch (Exception e2) {
			Global.logErrorMessage(e2);
		} finally {
			ctx.setAttachment(null);
			ctx.getChannel().close();
		}
	}
}
